package v1

import (
	"context"
	dv1 "jmicro/app/user/srv/internal/data/v1"
	v1 "jmicro/app/user/srv/internal/dataSearch/v1"
	"jmicro/app/user/srv/internal/domain/dto"
	metav1 "jmicro/pkg/common/meta/v1"
)

// service层的管理器
type userService struct {
	userStore       dv1.UserStore
	userSearchStore v1.UserSearchStore
}

var _ UserSrv = &userService{}

func NewuserService(us dv1.UserStore, usSch v1.UserSearchStore) UserSrv {
	return &userService{
		userStore:       us,
		userSearchStore: usSch,
	}
}

func (u *userService) List(ctx context.Context, opts metav1.ListMeta) (*dto.UserDTOList, error) {
	//这里是业务逻辑1
	/*
		1.data层的接口必须先写好
		2.我期望测试的时候每次测试底层的data层的数据按照我期望的返回
		 1.先手动插入一些数据
		 2.去删除一些数据
		 3.如果data层的方法有bug，坑爹 我们的代码想要具备好的可测试性
	*/
	doList, err := u.userStore.List(ctx, []string{}, opts)
	if err != nil {
		return nil, err
	}
	_, err = u.userSearchStore.List(ctx, opts)
	if err != nil {
		return nil, err
	}
	//业务逻辑2
	//代码不方便写单元测试用例
	var userDTOList = dto.UserDTOList{TotalCount: doList.TotalCount}

	for _, value := range doList.Items {
		projectDTO := dto.UserDTO{*value}
		userDTOList.Items = append(userDTOList.Items, &projectDTO)
	}
	//业务逻辑3
	return &userDTOList, nil
}
